﻿using BLL;
using PSIMIS2019.SubPages;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSIMIS2019.Templates
{
    public partial class Default : Form
    {
        public string UserID { get; set; }//用户ID
        public string Password { get; set; }//用户密码
        private SystemUserBLL bl;

        private Operation opera;//操作类型
        public Default()
        {
            InitializeComponent();
            bl = new SystemUserBLL();
        }
        /// <summary>
        /// 获取用户名、密码
        /// </summary>
        private void GetValues()
        {
            UserID = txtUserName.Text.Trim();
            Password = txtPassword.Text;
        }
        /// <summary>
        /// 用户名、密码输入的判断及错误处理信息提示
        /// </summary>
        private void Validation()
        {
            if (UserID.Length == 0 && Password.Length == 0)
            {
                MessageBox.Show("必须输入用户名和密码，否则无法登录！", "登录提示");
                txtUserName.Focus();
                return;
            }

            if (UserID == "" || UserID == null)
            {
                MessageBox.Show("用户名不能为空！请重新输入", "登录提示");
                txtUserName.Focus();
                return;
            }
            if (Password == "" || Password == null)
            {
                MessageBox.Show("密码不能为空！请重新输入", "登录提示");
                txtPassword.Focus();
                return;
            }
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            GetValues();
            Validation();
            if (bl.UserLogin(UserID, Password))
            {
                opera = Operation.LOGIN;
                UserName.Text = new EmployeeBLL().FindByID(UserID).EmployeeName;
            }
        }

        /// <summary>
        /// 通过泛型动态创建子窗体
        /// </summary>
        private T ShowMDIChildren<T>() where T : Form, new()
        {
            foreach (Form f in this.MdiChildren)//该循环可以防止同一个窗体重复打开
            {
                if (f is T)
                {
                    f.Activate();//激活窗体
                    f.WindowState = FormWindowState.Maximized;//将窗体最大化呈现
                    return f as T;
                }
            }
            T form = new T();//初始化泛型窗体
            form.MdiParent = this;//将泛型窗体的父一级窗体设定为当前窗体
            form.WindowState = FormWindowState.Maximized;//将窗体最大化呈现
            form.Show();
            return form;
        }



        private void 类型管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _Category form = ShowMDIChildren<_Category>();
        }

        private void 产品管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _Product form = ShowMDIChildren<_Product>();
        }

        private void 客户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _Customer form = ShowMDIChildren<_Customer>();
        }

        private void 供应商管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _Supplier form = ShowMDIChildren<_Supplier>();
        }

        private void 订单管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _PurchaseBill form = ShowMDIChildren<_PurchaseBill>();
        }

        private void 订单审核ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _ExaminPurchaseBill form = ShowMDIChildren<_ExaminPurchaseBill>();
        }

        private void 订单入库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _InStockPurchaseBill form = ShowMDIChildren<_InStockPurchaseBill>();
        }

        private void 关于我们ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("柳职2018软件技术2班荣誉出品！！", "系统信息");
        }

    }
}
